package cn.edu.tju.fctl.rest.aspect;

import cn.edu.tju.fctl.entities.LeaveDelay;
import cn.edu.tju.fctl.entities.LeaveLog;
import cn.edu.tju.fctl.rest.annotation.LeaveLogDetail;
import cn.edu.tju.fctl.service.ILeaveLogService;
import cn.edu.tju.fctl.service.impl.LeaveLogService;
import cn.edu.tju.fctl.util.BizCode;
import cn.edu.tju.fctl.util.EmailUtil;
import cn.edu.tju.fctl.util.ShiroUtil;
import cn.edu.tju.fctl.util.StateCode;
import com.sun.javafx.collections.MappingChange;
import com.sun.jersey.api.model.PathValue;
import javafx.util.Pair;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import org.springframework.validation.ObjectError;

import javax.ws.rs.PathParam;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Map;

/**
 * Created by catold on 15/12/25.
 *
 * leavelog日志切面
 */
@Aspect
@Component
@Configurable
public class LeaveLogInterceptor {
    @Autowired
    private ILeaveLogService leaveLogService;


    @AfterReturning(pointcut = "execution(* cn.edu.tju.fctl.rest.VacationRest.*(..)) " +
            "&& @annotation(cn.edu.tju.fctl.rest.annotation.LeaveLogDetail)", returning="result")
    public void addLeaveLog(JoinPoint joinPoint, Object result) {
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            LeaveLogDetail leaveLogDetail = method.getDeclaredAnnotation(LeaveLogDetail.class);
            // 判断返回是不是成功
            if (result instanceof StateCode && ((StateCode) result).getState() != 200) {
                // 不成功
                return;
            }

            LeaveLog leaveLog = new LeaveLog();
            leaveLog.setLog(leaveLogDetail.value());
            leaveLog.setUserId(ShiroUtil.getUser().getUserId());

            // 从参数中获取id
            Object args[] = joinPoint.getArgs();

            Parameter[] parameters = method.getParameters();
            for (int i = 0; i < parameters.length; i++){
                Parameter param = parameters[i];
                // 注解pathparam对应的值是id
                if((param.getDeclaredAnnotationsByType(PathParam.class).length == 0)){
                    continue;
                }
                if((param.getDeclaredAnnotationsByType(PathParam.class)[0]).value().equals("id")){
                    leaveLog.setLeaveId((Integer) args[i]);
                } else if((param.getDeclaredAnnotationsByType(PathParam.class)[0]).value().equals("delay_id")){
                    leaveLog.setLeaveDelayId((Integer) args[i]);
                }
            }

            if(!leaveLogDetail.hasId()){
                // 新添加的没有id
                // 从返回值中获取id

                Map<String, Object> data = ((StateCode) result).getData();
//                for(Pair<String, Object>)
                if(data != null && data.containsKey("leaveId")){
                    leaveLog.setLeaveId((Integer) data.get("leaveId"));
                }
                if(data != null && data.containsKey("delayId")){
                    leaveLog.setLeaveDelayId((Integer) data.get("delayId"));
                }
            }
            leaveLogService.create(leaveLog);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }
}
